/**
 * 层次遍历后判断是否是最右侧的节点，是就push进res中
 * @function
*/

const rightSideView = root => {
  const res = [],
    queue = []
  if (!root) return res
  queue.push(root)
  while (queue.length) {
    const cur = []
    let len = queue.length
    while (len--) {
      const node = queue.shift()
      if (!len) res.push(node.val)
      node.left && queue.push(node.left)
      node.right && queue.push(node.right)
    }
  }
  return res
}

const root = {
  val: 1,
  left: {
    val: 2,
    left: null,
    right: null
  },
  right: {
    val: 3,
    left: null,
    right: null
  }
}

console.log(rightSideView(root))